import { createApp } from 'vue'
import Toast from '@/components/feedback/Toast.vue'

// 实例对象
let toastInstance = null
let toastContainer = null
let toastApp = null

// 确保单例模式
const getInstance = () => {
  if (!toastInstance) {
    // 创建容器
    toastContainer = document.createElement('div')
    toastContainer.className = 'toast-container'
    document.body.appendChild(toastContainer)
    
    // 创建应用实例
    toastApp = createApp(Toast)
    toastInstance = toastApp.mount(toastContainer)
  }
  return toastInstance
}

// Toast服务方法
const toast = {
  // 显示轻提示
  show(options) {
    return getInstance().show(options)
  },
  
  // 信息提示
  info(message, duration) {
    return getInstance().info(message, duration)
  },
  
  // 成功提示
  success(message, duration) {
    return getInstance().success(message, duration)
  },
  
  // 警告提示
  warning(message, duration) {
    return getInstance().warning(message, duration)
  },
  
  // 错误提示
  error(message, duration) {
    return getInstance().error(message, duration)
  },
  
  // 加载提示
  loading(message, duration) {
    return getInstance().loading(message, duration)
  },
  
  // 隐藏提示
  hide() {
    if (toastInstance) {
      toastInstance.hide()
    }
  }
}

// 注册为插件
const ToastPlugin = {
  install(app) {
    app.config.globalProperties.$toast = toast
    app.provide('toast', toast)
  }
}

export default ToastPlugin
export { toast } 